#include "Clock.h"

namespace qtangram {
    Clock::Clock(QWidget *parent, Qt::WindowFlags f, const QString &text)
	: QLabel(text, parent, f) {
	timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(ref()));
    }
    void Clock::ref() {
	++secs;
	setText(getTime());
    }
    QString Clock::getTime() {
	QString hours, mins, sec;
	int h = static_cast<int>(secs / 3600);
	h < 10 ? hours = QString("0") + QString::number(h) : hours = QString::number(h);
	int m = static_cast<int>((secs - 3600 * h) / 60);
	m < 10 ? mins =  QString("0") + QString::number(m) : mins = QString::number(m);
	int s = static_cast<int>(secs - h * 3600 - m * 60);
	s < 10 ? sec =  QString("0") + QString::number(s) : sec = QString::number(s);
	return (hours + QString(":") +mins + QString(":") + sec);
    }

    void Clock::start() {
	secs = 0;
	timer->start(1000);
    }
    void Clock::pause() {
	if(timer->isActive()) stop();
	else timer->start();
    }
    void Clock::stop() {
	timer->stop();
    }
}
